var http        =       require('http');
var fs          =       require('fs');
var cheerio     =       require('cheerio');
var request     =       require('request').defaults({maxRedirects:100});
var mongoose    =       require('mongoose');

var url = "https://www.zhihu.com/question/";
var id = 0;

function fetchPage(){
    startRequest();
    id++;
}

function startRequest() {
    //console.log(url+id);
    request({
        url:url+id,
        gzip:true
        },
        function (error, response, body) {
            //console.log(response.statusCode);
            if (!error && response.statusCode == 200) {
                var $ = cheerio.load(body);
                console.log(body);
                var item = {
                    title : $('title').text(), //标题
                    ans_num : $('#zh-question-answer-num').text(), //答案数目
                    com_num : $('#z-icon-comment').text(), //评论数
                    focus_num : $('#zh-question-side-header-wrap').remove('button').text() //关注数
                }
                console.log("++++++++++++++++++++++++++++++++++++++++++++++");
                saveToMongoDb(item);
                console.log(item);
            }
            //限制条件
            if(id<100000000){
                return fetchPage();
            }else return false;
    })
}

function saveToMongoDb(item){
    var db = mongoose.createConnection('localhost','blog'); //创建一个数据库连接
    db.on('error',console.error.bind(console,'连接错误:'));
    //定义一个schema
    var QuestionSchema = new mongoose.Schema({
        title : String,   //定义一个属性name，类型为String
        ans_num : String,
        com_num : String,
        focus_num : String
    });
    //将该Schema发布为Model
    var QuestionModel = db.model('Question',QuestionSchema);
    //用Model创建Entity
    var questionEntity = new QuestionModel(item);
    questionEntity.save();
}

//主程序开始运行
fetchPage();
